package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class AdressBook here.
 * 
 * @author (Tommy Fransgaard) 
 * @version (08-11-2010)
 */
public class AddressBook
{
    private static AddressBook instance = new AddressBook();
    ArrayList<Person> personList;

    /**
     * Constructor for objects of class AdressBook
     */
    private AddressBook()   
    {
        personList = new ArrayList<Person>();
    }
    
    /**
     * There can be only one
     */
    public static AddressBook getInstance()
    {
        return instance;
    }
    
    /**
     * adds a person
     */
    public void addPerson(Person person)
    {
        personList.add(person);
    }
    
    /**
     * removes a person
     */   
    public void deletePerson(int ID)
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < personList.size() && !found)
        {
            person = personList.get(index);
            if(person.getId() == ID)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (found)
        {
            personList.remove(index);
        }
    }
    
    /**
     * finds a person object, through ID
     */
    public Person findPerson(int ID)
    {
        Person person = null;
        int index = 0;
        boolean found = false;
        while(index < personList.size() && !found)
        {
            person = personList.get(index);
            if(person.getId() == ID)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if (found)
        {
            return personList.get(index);
        }
        else
        {
            return null;
        }
    }

    public ArrayList<Person> getAllPersons() {
        return personList;
    }

}
